//
//  BCReactVC.swift
//  BCUIKit
//
//  Created by Basic on 2021/2/10.
//  react native 基类页面

import UIKit
import React
//import BCComConfigKit

public class BCReactVC : UIViewController {
    
    //MARK: - property
    /// rn开发调试的默认 bundle url，比如：http://192.168.0.33:8081
    public static var devBundleURL:String?
    /// rn默认 bundle name，比如：index.bundle
    public static var defaultBundleName:String = "index.bundle"
    
    //MARK: - init
    /// 根据 rn 模块初始化页面。如果debug模式下配置了 devBundleURL，则自动读取 devBundleURL ;反之读取 main bundle下的 defaultBundleName
    /// - Parameters:
    ///   - moduleName: 需要加载的模块名称
    ///   - initialProperties: 属性参数
    ///   - launchOptions: 选项
    /// - Returns: description
    public convenience init(moduleName:String, initialProperties:[AnyHashable:Any]?, launchOptions:[AnyHashable:Any]?=nil) {
        self.init()
        var jsBundleURL:URL?
        if let _devBundleURL = BCReactVC.devBundleURL {
            //远程调试 bundle
            jsBundleURL = URL(string: "\(_devBundleURL)/\(BCReactVC.defaultBundleName)?platform=ios")
        }
        else {
            //本地 bundle
            jsBundleURL = Bundle.main.url(forResource: BCReactVC.defaultBundleName, withExtension: nil)
        }
        if let _jsBundleURL = jsBundleURL {
            let rootView = RCTRootView(bundleURL: _jsBundleURL, moduleName: moduleName, initialProperties: initialProperties, launchOptions: launchOptions)
            self.view = rootView
        }
        else {
            #if DEBUG
            NSLog("[rn] init rn page error, js bundle not found,\(BCReactVC.devBundleURL ?? ""),\(BCReactVC.defaultBundleName)")
            #endif
        }
    }
    
    /// 根据 rn bundleURL、模块，初始化页面。如果未设置 bundleURL，则读取 main bundle下的 defaultBundleName。
    /// - Parameters:
    ///   - bundleURL: bundle url，如：http://192.168.0.33:8081/index.bundle?platform=ios
    ///   - moduleName: 需要加载的模块名称
    ///   - initialProperties: 属性参数
    ///   - launchOptions: 选项
    /// - Returns: description
    public convenience init(bundleURL:String?, moduleName:String, initialProperties:[String:Any]?, launchOptions:[String:Any]?=nil) {
        self.init()
        var jsBundleURL:URL?
        if let _bundleURL = bundleURL {
            //远程调试 bundle
            jsBundleURL = URL(string: _bundleURL)
        }
        else {
            //本地 bundle
            jsBundleURL = Bundle.main.url(forResource: BCReactVC.defaultBundleName, withExtension: nil)
        }
        if let _jsBundleURL = jsBundleURL {
            let rootView = RCTRootView(bundleURL: _jsBundleURL, moduleName: moduleName, initialProperties: initialProperties, launchOptions: launchOptions)
            self.view = rootView
        }
        else {
            #if DEBUG
            NSLog("[rn] init rn page error,js bundle not found,\(BCReactVC.devBundleURL ?? ""),\(BCReactVC.defaultBundleName)")
            #endif
        }
    }
    
    //MARK: - life cycle
    public override func viewDidLoad() {
        super.viewDidLoad()
    }
    
}
